/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package dal;

import dal.util.Database;
import java.sql.Date;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;
import java.util.logging.Level;
import java.util.logging.Logger;
import models.Messenger;

/**
 *
 * @author gabriellebotbol
 */
public class MessengersDAL 
{
    /**
     * returns a messenger by its username
     * @param username
     * @return 
     */
    public static ArrayList<Messenger> getMessengersByUsername(String username)
    {
        ArrayList<Messenger> messengers= new ArrayList<Messenger>();
        try 
        {
            ResultSet rs=Database.executeQuery("select * from Messenger where username='"+username+"';");
            while(rs.next())
            {
                messengers.add(new Messenger(UsersDAL.getUserByUsername(rs.getString("username")), rs.getInt("idMessenger"), rs.getString("body"), rs.getInt("nbLike"), rs.getInt("nbUnlike"), rs.getBoolean("isPublic"), rs.getBoolean("isSystem"), rs.getDate("date")));
                
            }
            Database.closeConnection();
        } 
        catch (SQLException ex) 
        {
            Logger.global.log(Level.SEVERE, null, ex);
        }
        return messengers;
    }
    
    /**
     * return a messenger by its username and gives begin and end dates
     * @param username
     * @param beginDate
     * @param endDate
     * @return 
     */
    public static ArrayList<Messenger> getMessengersByUsername(String username, Date beginDate, Date endDate)
    {
        ArrayList<Messenger> messengers= new ArrayList<Messenger>();
        try 
        {
            ResultSet rs=Database.executeQuery("select * from Messenger where username='"+username+"' AND date<"+beginDate+" AND date>"+endDate+";");
            while(rs.next())
            {
                messengers.add(new Messenger(UsersDAL.getUserByUsername(rs.getString("username")), rs.getInt("idMessenger"), rs.getString("body"), rs.getInt("nbLike"), rs.getInt("nbUnlike"), rs.getBoolean("isPublic"), rs.getBoolean("isSystem"), rs.getDate("date")));
                
            }
            Database.closeConnection();
        } 
        catch (SQLException ex) 
        {
            Logger.global.log(Level.SEVERE, null, ex);
        }
        return messengers;
    }

    /**
     * inserts a new message in the database
     * @param messenger 
     */
    public static void insertMessenger(Messenger messenger)
    {
        StringBuilder builder=new StringBuilder();
        builder.append("insert into Messenger(body,date,nbLike,nbUnlike,isPublic,isSystem,username) values('");
        builder.append(messenger.getBody());
        builder.append("','");
        builder.append(new Date(System.currentTimeMillis()));
        builder.append("',");
        builder.append(0);// à l'insertion il n'y a pas de like 
        builder.append(",");
        builder.append(0);
        builder.append(",");
        builder.append(messenger.getIsPublic());
        builder.append(",");
        builder.append(messenger.getIsSystem());
        builder.append(",'");
        builder.append(messenger.getUser().getUsername());
        builder.append("');");
        try 
        {
            Database.executeNonQuery(builder.toString());
        } 
        catch (SQLException ex) 
        {
            Logger.global.log(Level.SEVERE, null, ex);
        }
    }
    
    /**
     * delete a Messenger from the database
     * @param messenger 
     */
    public static void deleteMessenger(Messenger messenger)
    {
        try 
        {
            Database.executeNonQuery("delete from Messenger where idMessenger='"+messenger.getIdMessenger()+"';");
        } 
        catch (SQLException ex) 
        {
            Logger.global.log(Level.SEVERE, null, ex);
        }
    }      
}
